﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.IO;

namespace SPGhostFilesManager
{
    public class SPListFileItem
    {
        private SPListItem _item;

        public SPListFileItem(SPListItem item)
        {
            _item = item;
        }

        public string FileName
        {
            get
            {
                return _item.File.Name;
            }
        }

        public string Url
        {
            get
            {
                return _item.File.Url;
            }
        }

        public string SetupPath
        {
            get
            {
                return _item.File.Properties["vti_setuppath"] as string;
            }
        }

        public SPCustomizedPageStatus Status
        {
            get
            {
                return _item.File.CustomizedPageStatus;
            }
        }

        public string Save(string directory)
        {
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
            byte[] bytes = _item.File.OpenBinary();
            string filePath = Path.Combine(directory, _item.File.Name);
            System.IO.File.WriteAllBytes(filePath, bytes);
            return filePath;
        }

        public void Revert()
        {
            _item.File.RevertContentStream();
        }

        public void Ghost(string originalGhostedFileUrl)
        {
            _item.ParentList.ParentWeb.Site.RootWeb.GetFile(originalGhostedFileUrl).MoveTo(_item.File.Url, true);
        }
    }
}
